{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## _*Quantum Random Number Generation*_ \n",
    "\n",
    "The latest version of this notebook is available on https://github.com/QISKit/qiskit-tutorial.\n",
    "\n",
    "***\n",
    "### Contributors\n",
    "James R. Wootton, IBM Research\n",
    "***\n",
    "\n",
    "### Qiskit Package Versions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'qiskit': '0.10.3',\n",
       " 'qiskit-terra': '0.8.1',\n",
       " 'qiskit-ignis': '0.1.1',\n",
       " 'qiskit-aer': '0.2.1',\n",
       " 'qiskit-ibmq-provider': '0.2.2',\n",
       " 'qiskit-aqua': '0.5.1'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import qiskit\n",
    "qiskit.__qiskit_version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One important application of quantum computers is the generation of random numbers. This could be a simple as a coin flip."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Flip 1\n",
      "HEADS\n",
      "\n",
      "Flip 2\n",
      "HEADS\n",
      "\n",
      "Flip 3\n",
      "TAILS\n",
      "\n",
      "Flip 4\n",
      "HEADS\n",
      "\n",
      "Flip 5\n",
      "HEADS\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "for n in range(5):\n",
    "    print('Flip '+str(n+1))\n",
    "    if random.random()<0.5:\n",
    "        print('HEADS\\n')\n",
    "    else:\n",
    "        print('TAILS\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above code doesn't use Qiskit. Instead it uses an entirely classical package unimaginatively called `random`. There are many classical methods that can be used for this purpose, but none are truly random. Instead, they are *pseudorandom number generators*. They produce an output by taking a definite input and processing it in a deterministic way. These inputs are chosen such that the result appears random, and the processing is usually designed to accentuate this. But nevertheless, if you knew what was going on under the hood, you'd know exactly what random numbers would come out.\n",
    "\n",
    "For example, suppose we take an extremely precise measure of the time at which the random number was asked for. We could then look at the last digit (which changes most quickly), and assign our heads or tails depending on whether this is even or odd. Slight fluctations in the speed of our process will result in random seeming numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Flip 1  (system time = 1559624910.322222)\n",
      "HEADS\n",
      "\n",
      "Flip 2  (system time = 1559624910.322349)\n",
      "TAILS\n",
      "\n",
      "Flip 3  (system time = 1559624910.322529)\n",
      "TAILS\n",
      "\n",
      "Flip 4  (system time = 1559624910.3225758)\n",
      "HEADS\n",
      "\n",
      "Flip 5  (system time = 1559624910.32261)\n",
      "TAILS\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "for n in range(5):\n",
    "    t = str(time.time())\n",
    "    print('Flip '+str(n+1)+'  (system time = ' + t + ')')\n",
    "    if int(t[-1])%2==0:\n",
    "        print('HEADS\\n')\n",
    "    else:\n",
    "        print('TAILS\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This already does a fairly good job at seeming random, and more sophisticated methods will do even better, but all have their limits. This can be an important issue, since some applications rely on having good quality random numbers. For example:\n",
    "\n",
    "* Simulations of random processes may have skewed statistics for bad quality randomness;\n",
    "\n",
    "\n",
    "* Cryptography can be cracked if the key is created using poorly generated random numbers.\n",
    "\n",
    "We can solve these problems using the most genuine source of randomness that we know: measurements of quantum superpositions. Unlike a pseudorandom number generator, a qubit does not know what result it will give. It does not contain a definite set of details that tell it how to respond to any measurement it may experience. The result is completely undefined before the moment of measurement. This means we can use quantum computers for the ultimate coin flip."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum coin flip\n",
    "\n",
    "To generate a single random bit, we need only one qubit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit\n",
    "\n",
    "q = QuantumRegister(1)\n",
    "c = ClassicalRegister(1)\n",
    "circ = QuantumCircuit(q, c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We need to generate an equally weighted superposition of $|0\\rangle$ and $|1\\rangle$ to get a completely random bit. This is done most simply with the Hadamard."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<qiskit.circuit.instructionset.InstructionSet at 0x124d71b00>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ.h(q)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To extract the output, we then measure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<qiskit.circuit.instructionset.InstructionSet at 0x124d71d30>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ.measure(q, c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the circuit is complete, let's run it for 5 shots to get 5 results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import BasicAer, execute\n",
    "\n",
    "backend = BasicAer.get_backend('qasm_simulator')\n",
    "job = execute(circ, backend, shots=5, memory=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our results are extracted from `job.result()` using `get_memory()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['0', '0', '0', '0', '1']\n"
     ]
    }
   ],
   "source": [
    "data = job.result().get_memory()\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a coin flip, we can simply use `0` to mean heads and `1` to mean tails."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 1\n",
      "TAILS\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for output in data:\n",
    "    print('Flip with output bit value ' +output)\n",
    "    if output=='0':\n",
    "        print('HEADS\\n')\n",
    "    else:\n",
    "        print('TAILS\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We cheated above by using the simulator. Since this is a classical reproduction of a qubit, the results here were actually pseudorandom. For real randomness, we need a real device. So let's load an IBMQ account and use a 5 qubit quantum processor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import IBMQ\n",
    "from qiskit.providers.ibmq import least_busy\n",
    "\n",
    "IBMQ.load_accounts()\n",
    "\n",
    "backend = least_busy(IBMQ.backends(operational=True, simulator=False))\n",
    "job = execute(circ, backend, shots=5, memory=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 0\n",
      "HEADS\n",
      "\n",
      "Flip with output bit value 1\n",
      "TAILS\n",
      "\n",
      "Flip with output bit value 1\n",
      "TAILS\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for output in job.result().get_memory():\n",
    "    print('Flip with output bit value ' +output)\n",
    "    if output=='0':\n",
    "        print('HEADS\\n')\n",
    "    else:\n",
    "        print('TAILS\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the list of results as we get it from `get_memory()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0', '0', '0', '1', '1']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "job.result().get_memory()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that noise results in these results typically showing a bias towards `0`. So some post-processing is required if these numbers are to be used for statistical studies. Also, though the universe ensures the cryptographic security of these numbers, their journey between the qubit and your computer might not be so secure. You'll need to take additional precautions before using these results for security applications."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## More general probability distributions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes we need something more complex than just a coin flip. Many applications require generation of many possible numbers within a certain range (rather than just $0$ and $1$), and to do according probability distribitions for which each outcome could occur with a different probability.\n",
    "\n",
    "This is also possible for quantum computers. In fact, quantum computers will be able to efficiently sample from probability distributions that would be intractable for classical computers.\n",
    "\n",
    "To represent numbers larger than $0$ and $1$, we need more qubits. The $n$ bit string from the output of $n$ qubits can then be interpreted as the binary representation of numbers from $0$ to $2^n-1$. For example, let's choose $n=3$ to generate numbers from $0$ to $7$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a uniform distribition over all these numbers, we could simply do a Hadamard on each. Here's an implementation of this, with the histogram obtained from many samples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q = QuantumRegister(n)\n",
    "c = ClassicalRegister(n)\n",
    "circ = QuantumCircuit(q, c)\n",
    "\n",
    "for j in range(n):\n",
    "    circ.h(q[j])\n",
    "    \n",
    "circ.measure(q,c)\n",
    "\n",
    "job = execute(circ, BasicAer.get_backend('qasm_simulator'), shots=8192)\n",
    "\n",
    "# get the histogram of bit string results, convert it to one of integers and plot it\n",
    "bit_counts = job.result().get_counts()\n",
    "int_counts = {}\n",
    "for bitstring in bit_counts:\n",
    "    int_counts[ int(bitstring,2) ] = bit_counts[bitstring]\n",
    "\n",
    "from qiskit.tools.visualization import plot_histogram    \n",
    "plot_histogram(int_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's try something slightly more complex, like a bias towards smaller numbers. This can be done by using the `rx` rotation on each qubit, by using an angle for each `rx` that decreases as we go from `q[0]` (whose output corresponds to the least significant binary digit) to `q[n-1]` (the most significant).\n",
    "\n",
    "Below is one possible implementation of this. It was chosen fairly arbitrarily, so why not try your own variation?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAc0AAAEyCAYAAACYgYvRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de3xV1Z338c/PcEcQgogJGgKCGEFjCA8vLhKwGB5wvFDMy0tppfK0XvDSFkec+mixnZbOYBnRjlZsZbC1naJDRuuF6oNQmQpWDRgvRApGgkDUCCrIJSHh9/yxDzEJSdiH5FwC3/frdV6cvfbaO799zuH8zlp77bXN3REREZEjOyHRAYiIiLQVSpoiIiIhKWmKiIiEpKQpIiISkpKmiIhISEqaIiIiIbVLdACJ1KtXL8/IyEh0GCIikkTefPPNT929d2PrjuukmZGRwYoVKxIdhoiIJJHU1NSyptape1ZERCQkJU0REZGQlDRFRERCUtIUEREJSUlTREQkJCVNERGRkJQ0RUREQlLSFBERCUlJU0REJCQlTRERkZCUNEVEREJS0hQREQlJSVNERCQkJU0REZGQlDRFRERCUtIUEREJSUlTREQkJCVNERGRkJQ0RUREQlLSFBERCUlJU0REJCQlTRERkZCUNFto+fLljBgxgtzcXBYsWNBkvT/96U+kpqaybt262rJ3332XiRMnMmrUKMaMGcP+/fsBuOSSSxgxYgR5eXnk5eVRUVER8+MQEZEja5foANqympoaZs+eTWFhIenp6UyYMIFJkyZx1lln1au3e/duFi5cSG5ubm1ZdXU1119/PQ8//DBDhw5l586dtG/fvnb9woULycnJiduxiIjIkaml2QJFRUX079+fzMxMOnTowNSpU1m2bNlh9ebOncv3vvc9OnXqVFu2cuVKhgwZwtChQwFITU0lJSUlbrGLiEj0lDRboLy8nL59+9Yup6enU15eXq9OcXEx27ZtY+LEifXKN23ahJlx+eWXM378eB544IF662+++Wby8vK49957cffYHYSIiISm7tkYOnjwIHfddRcPPvjgYeuqq6t59dVXeemll+jcuTNTpkwhOzubcePGsXDhQtLT09m9ezfTp09nyZIlXHXVVQk4AhERqUstzRZIS0tj27Zttcvbt28nLS2tdvnLL7+kpKSESy65hOzsbN544w2mTZvGunXrSE9PZ/To0fTq1YsuXbqQn59PcXExELRYAbp160ZBQQFr166N74GJiEijlDRbYNiwYZSWllJWVkZVVRWFhYVMmjSpdn337t3ZtGkTxcXFFBcXM3z4cH7/+9+Tk5PDhAkTWL9+PXv37qW6uprVq1dz1llnUV1dzY4dOwA4cOAAL7zwA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HMLDYHAOTn55Ofn1+v7M4776x93qlTJxYvXtzotldccQVXXHFFvbKuXbuycuXKVo9TpLWpe1Ykzloy+hSCLtpx48Zx8cUXs2bNmnrbXX755Zx55pmceOKJXHbZZTE+EpHjj5KmSBvSp08f3nrrLV5++WV++tOf8t3vfpddu3bVrl+6dCklJSVUVlY22oUrIi2j7lmROItm9Gnfvn3rjT41Mzp27AjAeeedR//+/Xn//ffrzaLTqVMnLrroIpYtW8YFF1wQk2OY/VjPmOz3kHnTP4vp/kWOllqaInHWktGnn376ae2o2M2bN1NaWkpmZiZffvklH330ERCc03zxxRcZNGhQfA9M5DiglqZInLVk9Onq1av5+c9/Tvv27TnhhBOYP38+PXv25JNPPmHatGlUVlZy8OBBxo4dy7XXXpvgIxU59ihpiiTA0Y4+vfTSS7n00ksPKz/llFN46aWXWj1OEalP3bMiIiIhKWmKiIiEpO5ZkQTRCNTEWL58OXfeeSc1NTV861vf4vvf/3699ZWVldx4440UFxfTs2dPFi1aREZGBkVFRfzgBz8AwN254447uPjii9m6dSszZ87kk08+wcyYPn06N9xwQyIOTeJASVNEjhstmcIwKyuLFStW0K5dOz766CPy8vKYNGkS7dq145//+Z/Jzs5m9+7dfO1rX2P8+PH19inHDnXPishxoyU30O7SpQvt2gXtjMrKytppCk899VSys7OB4A4tZ5555mEzPMmxQ0lTRI4bLZ3C8I033mDUqFGcf/75zJ8/vzaJHrJlyxbeeustcnNzY3wkkihKmiIiIQ0fPpw1a9awfPlyFixYwP79+2vXffnll9AtT9UAAAu1SURBVEyfPp25c+fWTp4vxx4lTRE5brTkBtp1DR48mK5du1JSUgIE9wCdPn06BQUFXHLJJTE+CkkkJU0ROW60ZArDsrIyqqurAfjwww/ZuHEjGRkZuDu33norZ555JjfddFPcj0niS6NnReS40ZIpDF999VUWLFhQO4XhvffeS69evXj11VdZsmQJZ599Nnl5eQDcfffdh834JMcGJU0ROa4c7RSGV155JVdeeeVh5SNHjqwdKCTHPnXPioi0EcuXL2fEiBHk5uayYM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gODGzUwi+tKcQDDZ4B3jS3d9LaGBRiHS9mbvXmNl3gfvdvUui42pOpLV5aMh9e4JRqWcQnCfJaHbjJHHoGCKv/7eAn7h7v0THdSSRVv88gvOa3/evupyTmpl1IPhhtc/dP4yUnUCQUH/i7knXWjskcnnVFQQDyHoRnP9OJUhMT7r7rASGF5UG/3e/Dcxw97zERqXRswlhZid4kvTPHy0zm0XQarg30bGEUSfxXE1wbvYed/9JouOKhplNBW4Hlrj7gkTHE0YkAZ3o7jsTHUtLmFkBwemW37v7LxIdTxhm1o9gNKoTtPj/6u7ViY3q6JlZT3f/LOFxKGnK0Yi02mraWvKPnJsaDpR6El4q05xI7BlAhbvvTXQ8x5PIa58JfOJJdpmSxJeSpoiISEgaPSsiIhKSkqaIiEhISpoiIiIhKWmKiIiEpKQpIiISkpKmiIhISP8fUqojH2lkHQsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q = QuantumRegister(n)\n",
    "c = ClassicalRegister(n)\n",
    "circ = QuantumCircuit(q, c)\n",
    "\n",
    "for j in range(n):\n",
    "    circ.rx(3.14159/(2.5+j),q[j])\n",
    "    \n",
    "circ.measure(q,c)\n",
    "    \n",
    "job = execute(circ, BasicAer.get_backend('qasm_simulator'), shots=8192)\n",
    "\n",
    "bit_counts = job.result().get_counts()\n",
    "int_counts = {}\n",
    "for bitstring in bit_counts:\n",
    "    int_counts[ int(bitstring,2) ] = bit_counts[bitstring]\n",
    "plot_histogram(int_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's get some actual random samples from this distribution. Specifically, let's get ten of them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['000', '000', '001', '001', '001', '001', '001', '111', '001', '001']\n"
     ]
    }
   ],
   "source": [
    "job = execute(circ, BasicAer.get_backend('qasm_simulator'), shots=10, memory=True)\n",
    "data = job.result().get_memory()\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These are written in binary, but it is straightforward to convert them into decimal integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 1, 1, 1, 1, 1, 7, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "int_data = []\n",
    "for bitstring in data:\n",
    "    int_data.append( int(bitstring,2) )\n",
    "print(int_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we see, as we expected, a bias towards the smaller numbers in the range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
